import javax.management.ListenerNotFoundException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28956
 * Date: 2024-07-20
 * Time: 13:38
 */
//队列模拟实现（双向链表）
public class MyLinkQueue {
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    public int usedSize;

    //入队（尾插法）
    public boolean offer(int val){
        ListNode node = new ListNode(val);
        if(head == null){
            head = node;
            last = node;
        }else{
            last.next = node;
            node.prev = last;
            last = node;
            usedSize++;
        }
        return true;
    }

    //出队（删除头结点）
    public int poll(){
        if(head == null){
            return -1;
        }
        int retVal = head.val;
        if(head.next == null){
            head = null;
            last = null;
            return retVal;
        }
        head = head.next;
        head.prev = null;
        return retVal;
    }

    public int peek(){
        if(head == null){
            return -1;
        }
        return head.val;
    }

    public boolean empty(){
        return head == null;
    }

    public int size(){
        return usedSize;
    }
}
